package com.trkj.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.dao.jpa.IEplanDetailDao;
import com.trkj.dao.jpa.IEquipPlanDAO;
import com.trkj.dao.mybatis.IEplanDAO;
import com.trkj.dao.mybatis.IEplanDetailsMapper;
import com.trkj.pojos.EplanDetail;
import com.trkj.pojos.EquipPlan;

@Service
@Transactional
public class EquipPlanService {
	@Autowired
	private IEquipPlanDAO planDao;
	@Autowired
	private IEplanDetailDao plandetailDao;
	@Autowired
	private IEplanDAO ePlanDAO;
	@Autowired
	private IEplanDetailsMapper detailsMapper;
	/**
	 * 添加计划
	 * @param plan 
	 * @param planDetails
	 */
	public boolean addPlan(EquipPlan eplan, List<EplanDetail> planDetails){
		try {
			planDao.save(eplan);		
			//绑定关系
			if(planDetails != null){
				for(EplanDetail detail: planDetails){
					detail.setEquipPlan(eplan);
				}
			}		
			plandetailDao.save(planDetails);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 修改计划
	 * @param plan 
	 * @param planDetails
	 */
	public boolean editePlan(long eplanId,EquipPlan eplan, List<EplanDetail> planDetails){
		try {
			EquipPlan equipPlan = planDao.findOne(eplanId);
			equipPlan.setEplanAuthor(eplan.getEplanAuthor());
			equipPlan.setEplanDate(eplan.getEplanDate());
			equipPlan.setEplanDept(eplan.getEplanDept());
			equipPlan.setEplanTitle(eplan.getEplanTitle());
			equipPlan.setEplanType(eplan.getEplanType());
			List<EplanDetail> eplanDetails = equipPlan.getEplanDetails();
			for (EplanDetail eplanDetail : eplanDetails) {
				/*long edetailId = eplanDetail.getEdetailId();*/
				plandetailDao.delete(eplanDetail);
			}
			//绑定关系
			if(planDetails != null){
				for(EplanDetail detail: planDetails){
					detail.setEquipPlan(equipPlan);
				}
			}		
			plandetailDao.save(planDetails);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 查询计划分頁
	 * 
	 */
	public Map<String,Object> getEplan(int pageNum,int pageSize){
		Page<EquipPlan> p = PageHelper.startPage(pageNum, pageSize, true);
		List<EquipPlan> list = ePlanDAO.selectEplanAll();
		
		Map<String,Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", list);
		return map;
	}
	/**
	 * 查询计划按照id
	 * 
	 */
	public Map<String,Object> getEplanOne(long eplan_id){
		EquipPlan equip = planDao.findOne(eplan_id);
		Map<String,Object> map = new HashMap<>();
		map.put("equip", equip);
		return map;
	}
	/**
	 * 查询计划詳情按照eplan_id
	 * 
	 */
	public Map<String,Object> getEplanDetail(String eplan_id,int pageNum,int pageSize){
		Page<EquipPlan> p = PageHelper.startPage(pageNum, pageSize, true);
		Map<String,Object> map =new HashMap<>();
		map.put("eplanId", eplan_id);
		List<EplanDetail> detail = detailsMapper.findEplanDetails(map);

		Map<String,Object> map1 = new HashMap<>();
		map1.put("total", p.getTotal());
		map1.put("rows", detail);
		return map1;
	}
	/**
	 * 条件查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public Map<String,Object> getEplanWhere(int pageNum,int pageSize,String eplanType,String eplanDate,String eplanAuthor,String eplanTitle){
		Page<EquipPlan> p = PageHelper.startPage(pageNum, pageSize, true);
		Map<String,Object> where= new HashMap<>();
		where.put("eplanType", eplanType);
		where.put("eplan_author", eplanAuthor);
		where.put("eplan_date", eplanDate);
		where.put("eplan_title", eplanTitle);
		
		List<EquipPlan> list = ePlanDAO.selectWhereEplan(where);
		
		Map<String,Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", list);
		return map;
	}
	/**
	 * 删除计划
	 * @param eplan_id
	 * @return 
	 * @return
	 */

	public boolean deletePlan(long eplanId){
			EquipPlan one = planDao.findOne(eplanId);
			String eplanState = one.getEplanState();
			if(eplanState.equals("已审核")||eplanState.equals("已订购")){
				return false;
			}else if(one.getEplanDetails() != null){
				List<EplanDetail> eplanDetails = one.getEplanDetails();
				for(EplanDetail detail: eplanDetails){
					long edetailId = detail.getEdetailId();
					EplanDetail eplanDetail = plandetailDao.findOne(edetailId);
					plandetailDao.delete(eplanDetail);
				}
				planDao.delete(one);
			}
			return true;
			
	}
	/**
	 * 审核计划
	 * @param eplanId
	 * @return 
	 */
	public boolean CheckedEplan(long eplanId) {
		try {
			EquipPlan one = planDao.findOne(eplanId);	
			one.setEplanState("已审核");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 查询已审核的计划
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public  Map<String,Object> getCheckedEplan(int pageNum,int pageSize){
		Page<EquipPlan> p = PageHelper.startPage(pageNum, pageSize, true);
		List<EquipPlan> list = ePlanDAO.getCheckedEplan();
		Map<String,Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", list);
		return map;		
	}
	/**
	 * 条件查询已审核的计划
	 * @param page
	 * @param rows
	 * @param eplanType
	 * @param eplanDate
	 * @param eplanDept
	 * @return
	 */
	public Map<String, Object> getCheckedEplanWhere(int page, int rows, String eplanType, String eplanDate,
			String eplanDept) {
		Page<EquipPlan> p = PageHelper.startPage(page, rows, true);
		
		Map<String,Object> where= new HashMap<>();
		where.put("eplanType", eplanType);
		where.put("eplan_date", eplanDate);
		where.put("eplan_dept", eplanDept);
		List<EquipPlan> list = ePlanDAO.checkedWhereEplan(where);
		Map<String,Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", list);
		return map;		
	}
	public Map<String, Object> getEplanDetail(String eplan_id) {
		Map<String,Object> map =new HashMap<>();
		map.put("eplanId", eplan_id);
		List<EplanDetail> detail = detailsMapper.findEplanDetails(map);

		Map<String,Object> map1 = new HashMap<>();
		/*map1.put("total", detail.size());*/
		map1.put("rows", detail);
		return map1;
	}

}
